/**
 * Main
 * 
 * 04/12/2013
 *
 * Copyright GPL
 */
package jtm.project.exercise;

import java.util.Scanner;

/**
 * This is the main class.
 * 
 * @author Javier Tejedor
 */
public class Main {

	/**
	 * This is the point of entry to the program.
	 * @param args
	 */
	public static void main(String[] args) {

		Scanner reader = new Scanner(System.in);

		mainMenu(reader);
	}

	/**
	 * main menu
	 * @param roleDAO
	 * @param userDAO
	 * @param reader
	 */
	private static void mainMenu(Scanner reader) {
		String option= "";
		int idRol = 0;
		do{
			System.out.println(
					"Introduce una opcion(1:Introducir nuevo rol | S:Salir)");
			option = reader.nextLine();

			switch (option) {
			case "1":
				idRol = addRol(reader);
				addUser(reader, idRol);
				break;
				
			default:
				break;
			}

		} while(!option.toUpperCase().equals(("S").toUpperCase()));
	}

	
	/**
	 * this adds a role
	 * @param roleDAO
	 * @param reader
	 */
	private static int addRol(Scanner reader) {
		String name = "";
		String description = "";
		int id = 0;
		
		System.out.println("ADD ROLE");
		
		System.out.println("Role's name:");
		name = reader.nextLine();
		
		System.out.println("Role's description:");
		description = reader.nextLine();
		
		id = RoleDAO.getRoleDAO().insert(new Role(0, name, description));
		System.out.println("Role inserted");
		return id;
	}
	
	/**
	 * this adds a user
	 * @param userDAO
	 * @param reader
	 * @param idRol
	 */
	private static void addUser(Scanner reader, int idRol) {
		String login = "";
		String password = "";
		String email = "";
		
		System.out.println("\nADD USER TO ROLE " + idRol);
		
		System.out.println("User's login:");
		login = reader.nextLine();
		
		System.out.println("User's email:");
		email = reader.nextLine();
		
		System.out.println("User's password:");
		password = reader.nextLine();
		
		UserDAO.getUserDAO().insert(
				new User(
				0, login, password, email, idRol, RoleDAO.getRoleDAO().
				selectById(idRol)));
		
		System.out.println("User inserted");
	}

}
